<script setup>
import { useOrderStore, useUserStore } from '@/stores/index'
import { baseURL } from '@/utils/request'
import { ElMessage, ElMessageBox } from 'element-plus'
import { WarnTriangleFilled } from '@element-plus/icons-vue'
import { ref } from 'vue'
const orderStore = useOrderStore()
const userStore = useUserStore()
const orderList = ref([])

const getOrderList = async (id) => {
  await orderStore.getOrderList(id)
  console.log(orderStore.order)
  orderList.value = orderStore.order.filter((item) => item.shopstatic === 1)
  console.log(orderList.value)
}
getOrderList(userStore.user.id)
const formatDate = (isoDateString) => {
  const date = new Date(isoDateString)
  return date.toLocaleString() // 转换为本地时间格式
}
const deleteOrder = async (id) => {
  await ElMessageBox.confirm(`你确定要删除这个订单吗?`, '温馨提示', {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    type: 'warning',
    icon: WarnTriangleFilled,
    draggable: true
  })
  await orderStore.changeOrderStatus(id)
  await getOrderList(userStore.user.id)
  ElMessage.success('删除成功')
}
</script>

<template>
  <div class="main-container">
    <div>
      <div class="title">
        <div class="left">
          <div class="iconfont">
            <el-icon><List /></el-icon>
          </div>
          <span>订单界面</span>
        </div>

        <div class="right" @click="$router.push('/unpayorder')">未付款</div>
      </div>
      <div class="order-list">
        <div class="title">订单列表</div>
        <div class="main">
          <ul v-if="orderList.length !== 0">
            <li v-for="item in orderList" :key="item.id">
              <!-- 表头 -->
              <div class="tr th">
                <div class="td tdname">商品名称</div>
                <div class="right">
                  <div class="td tdorder">订单号</div>
                  <div class="td tdtime">下单日期</div>
                  <div class="td tdcount">优惠</div>
                  <div class="td tdtotal">原价</div>
                  <div class="td tdstate">订单状态</div>
                </div>
              </div>
              <!-- 订单商品 -->
              <ul>
                <li>
                  <div class="tr">
                    <div class="td tdname">
                      <a href="#" target="_blank" class="img"
                        ><img
                          alt=""
                          :src="baseURL + '/files/' + item.image"
                          style="display: block" /></a
                      ><a href="#" target="_blank" class="name_p"
                        ><span>{{ item.goodname }}</span></a
                      >
                    </div>
                    <div class="td tdorder">{{ item.goodId }}</div>
                    <div class="td tdtime">
                      {{ formatDate(item.createtime) }}
                    </div>
                    <div class="td tdcount">0</div>
                    <div class="td tdtotal">¥{{ item.goodprice }}</div>
                    <div class="td tdstate">
                      {{ item.shopstatic === 1 ? '已支付' : '未支付' }}
                    </div>
                  </div>
                  <div class="btn">
                    <!-- 删除历史订单 -->
                    <el-button type="danger" size="mini" @click="deleteOrder(item.id)"
                      >删除</el-button
                    >
                  </div>
                </li>
              </ul>
            </li>
          </ul>
          <template v-else>
            <el-empty description="您还没有添加订单" />
          </template>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped lang="scss">
.main-container {
  background-color: #fff;
  border-radius: 0 0 10px 10px;
  margin: 0 auto;
  width: 1200px;
  padding-top: 20px;
  height: 100vh;

  .title {
    padding: 10px 30px;
    display: flex;
    justify-content: space-between;
    align-items: center;

    .left {
      font-size: 24px;
      display: flex;

      .iconfont {
        width: 40px;
        height: 40px;

        .el-icon {
          box-sizing: border-box;
          font-size: 40px;
        }
      }

      span {
        margin-left: 10px;
        line-height: 40px;
      }
    }

    .right {
      width: 80px;
      height: 40px;
      font-size: 16px;
      font-weight: 500;
      color: #000000;
      background: linear-gradient(90deg, #faddab 0%, #c89556 100%);
      text-align: center;
      line-height: 40px;
      border-radius: 0 20px 0 20px;
      cursor: pointer;
      transition: all 0.3s ease;

      &:hover {
        color: #f83314;
        border: #f83314 1px solid;
        transform: scale(0.95);
      }
    }
  }
  .order-list {
    padding: 30px;
    /* background-color: #b66969; */
    .title {
      font-size: 24px;
      color: #000000;
    }
    .main {
      /* background-color: green; */
      ul {
        li {
          margin-top: 20px;
          margin-bottom: 60px;
          padding: 20px;
          background-color: #fff;

          border: gray 1px solid;
          &:hover {
            border: #f83314 1px solid;
          }
          .tr.th {
            display: flex;
            margin-left: 20px;
            text-align: center;

            .td.tdname {
              width: 400px;
              padding-right: 70px;
            }

            .right {
              flex: 1;
              display: flex;
              margin-right: 10px;
              align-items: center;

              .td.tdorder {
                width: 130px;
                padding-right: 70px;
              }

              .td.tdtime {
                width: 160px;
                margin-right: 80px;
              }
              .td.tdcount {
                margin-right: 50px;
              }
              .td.tdtotal {
                margin-right: 90px;
              }
            }
          }

          ul {
            border-radius: 20px;

            li {
              border: none;
              &:hover {
                border: none;
              }
            }
            .btn {
              margin-top: 40px;
              float: right;
              margin-right: 5px;
            }
            .tr {
              height: 124px;
              display: flex;
              width: 100%;
              border-radius: 25px;
              justify-content: space-between;
              background-color: rgb(245, 248, 250);

              .tdname {
                display: flex;
                align-items: center;

                .img {
                  img {
                    height: 120px;
                    border-radius: 15px;
                    margin-right: 20px;
                    transition: all 0.3s ease;

                    &:hover {
                      transform: scale(1.1);
                    }
                  }
                }

                .name_p {
                  font-size: 16px;
                  width: 180px;

                  &:hover {
                    color: #de4815;
                  }
                }
              }

              .td {
                display: flex;
                margin-right: 20px;
                align-items: center;
                justify-content: space-around;
              }

              .td.tdtotal {
                font-size: 20px;
                font-weight: bold;
              }
            }
          }
        }
      }
    }
  }
}
</style>
